package com.cdshangpai.photoapp.ui.caseui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.cdshangpai.photoapp.databinding.ActivityCaseListBinding;
import com.cdshangpai.photoapp.ui.CaseFilter;
import com.cdshangpai.photoapp.ui.OnListViewClickListener;
import com.cdshangpai.photoapp.ui.ViewMarginAdjust;
import com.cdshangpai.utils.db.CaseDb;
import com.cdshangpai.utils.db.beans.Case;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.List;

public class CaseFragment extends Fragment {
    ActivityCaseListBinding binding;

    CaseFilter caseFilter=new CaseFilter();
    CaseDb caseDb;


    private OnListViewClickListener onListViewClickListener=new OnListViewClickListener() {
        @Override
        public void onClick(Integer position) {
            Case cas=binding.getData().adapter.datas.get(position);
            String caseString= JSONObject.toJSONString(cas);

            Intent intent=new Intent(getContext(),CaseDetailActivity.class);
            intent.putExtra("param",caseString);
            startActivity(intent);
        }
    };

    public CaseFragment()
    {
        caseDb=new CaseDb();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding= ActivityCaseListBinding.inflate(inflater);
        binding.caseListView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        binding.setView(this);
        binding.setNonedata(false);

        initData();


        View view=binding.getRoot();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ViewMarginAdjust.adjustListPosition(binding.caseListView);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
         if(!hidden)
        {
            search();
        }
    }

    public void search()
    {
        new Thread(){
            @Override
            public void run() {
                List<Case> cases=caseDb.query(caseFilter.area,caseFilter.city,caseFilter.style,caseFilter.color,caseFilter.price);
                binding.getData().adapter.datas=cases;
                CaseFragment.this.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.caseListView.setAdapter(binding.getData().getAdapter());
                        binding.setNonedata(cases.size()==0);
                    }
                });
            }
        }.start();
    }

    /**
     * 根据关键字查询
     * @param keyword
     */
    public void keywordSearch(String keyword)
    {

        new Thread(){
            @Override
            public void run() {
                List<Case> cases=caseDb.queryByKeyword(keyword);
                binding.getData().adapter.datas=cases;
                CaseFragment.this.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.caseListView.setAdapter(binding.getData().getAdapter());
                        binding.setNonedata(cases.size()==0);
                    }
                });
            }
        }.start();
    }

    private FlexboxLayoutManager getFlexboxLayoutManager(Boolean isCenter)
    {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this.getContext());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        if(isCenter)
        {
            layoutManager.setJustifyContent(JustifyContent.CENTER);
            layoutManager.setAlignItems(AlignItems.CENTER);
        }
        else {
            layoutManager.setAlignItems(AlignItems.STRETCH);
            layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        }
        return layoutManager;
    }

    /**
     * 初始化案例数据
     */
    private void initData() {
        CaseListUiData caseListUiData = new CaseListUiData();
        ImageListAdapter imageListAdapter = new ImageListAdapter(this.getContext(),onListViewClickListener);
        caseListUiData.adapter = imageListAdapter;
        binding.setData(caseListUiData);

        FlexboxLayoutManager layoutManager=getFlexboxLayoutManager(false);
        binding.caseListView.setLayoutManager(layoutManager);

        search();
    }
}
